//
//  ZCMeRaiseVC.m
//  zctx
//
//  Created by apple on 15/10/13.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMeRaiseVC.h"
#import "ZCMeAuditDetailsVC.h"
#import "ZCMainModel.h"

#import "ZCmainCell.h"
#import "ZCProjectdetailsVC.h"

#import "ZCInvolvedCell.h"
#import "ZCInvolvedDetailsVC.h"

#import "ZCSamplingDetailVC.h"
#import "ZCSamplingCell.h"

@interface ZCMeRaiseVC ()

@end

@implementation ZCMeRaiseVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    if (UserModel.password.length>1) {
//        [self requestSeeResultdata];
//    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"发起的";
    
    [self initnavegation];
    
    dataArray = [[NSMutableArray alloc]init];
    
    [self requestSeeResultdata]; //请求数据
    
    [self initTableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCmainCell" bundle:nil] forCellReuseIdentifier:@"maincell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSamplingCell" bundle:nil] forCellReuseIdentifier:@"samcell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCInvolvedCell" bundle:nil] forCellReuseIdentifier:@"involvedcell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 26, 25);
    [rightButton setImage:[UIImage imageNamed:@"navigation_search"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(searchButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = barButtonItem_rightNavBar;
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 请求结果数据
-(void)requestSeeResultdata{
    
    ProgressHUDStr;
    [NetworkManager requestFoundingGetAllWithType:@"0" mid:UserModel.mid password:UserModel.password mtype:@"1" pagecount:@"0" keywords:@"" success:^(NSArray *array) {
        ProgressDismiss;
//        if (contentArray.count>0) {
            dataArray = array.mutableCopy;
            [self.tableView reloadData];
//        }
    } failure:^(NSError *error) {
        ProgressDismiss;
    }];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  dataArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCMainModel *moel =[ZCMainModel modelObjectWithDictionary:dataArray[indexPath.row]];
    NSString *resultStr = [NSString stringWithFormat:@"%@",moel.result];
    NSString *stateStr = [NSString stringWithFormat:@"%@",moel.state];
    
//    NSLog(@"result = %@,state = %@",resultStr,stateStr);
    if ([resultStr isEqualToString:@"0"]) {
        if ([stateStr isEqualToString:@"1"])
        {
            static NSString *cellid =@"involvedcell";
            ZCInvolvedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (cell == nil) {
                cell = [[ZCInvolvedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            cell.mainModel = moel;
            return cell;
        }
        else
        {// 0 审核中  2 抽样中 3完成 4 异常
            static NSString *cellid =@"samcell";
            ZCSamplingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (cell == nil) {
                cell = [[ZCSamplingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            cell.mainModel = moel;
            return cell;
        }
    }
    static NSString *cellid =@"maincell";
    ZCmainCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[ZCmainCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.mainModel = moel;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ZCMainModel *moel =[ZCMainModel modelObjectWithDictionary:dataArray[indexPath.row]];
    
    NSString *mid =[NSString stringWithFormat:@"%@",moel.contentIdentifier];
    NSString *resultStr = [NSString stringWithFormat:@"%@",moel.result];
    NSString *stateStr = [NSString stringWithFormat:@"%@",moel.state];

//    NSLog(@"result = %@,state = %@",resultStr,stateStr);
    if ([resultStr isEqualToString:@"0"]) {
        if ([stateStr isEqualToString:@"0"]) {
            ZCMeAuditDetailsVC *detailVC= [[ZCMeAuditDetailsVC alloc]init];
            detailVC.pid = mid;
            detailVC.titlteStr = moel.pjname;
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
        else if([stateStr isEqualToString:@"1"])
        {
            ZCInvolvedDetailsVC *detailVC = [[ZCInvolvedDetailsVC alloc]init];
            detailVC.pid = mid;
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
        else if([stateStr isEqualToString:@"2"])
        {
            ZCSamplingDetailVC *detailVC = [[ZCSamplingDetailVC alloc]init];
            detailVC.pid = mid;
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
    }else{
        ZCProjectdetailsVC *detailVC = [[ZCProjectdetailsVC alloc]init];
        detailVC.pid = mid;
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}
#pragma mark 搜索
-(void)searchButtonPressed{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
